

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="前言做手机游戏运营难免会遇到上传超大APP，但受限于宽带、http响应时间等因素，导致文件上传不成功。在网上搜罗了一圈，终于在github上找到了kazaff大神对于webuploader大文件上传的详细介绍。 下面整理一下自己的实现过程吧（kazaff已做了详细介绍，这里只是借鉴一下）：文件目录结构： index.html1234567891011121314151617181920212223">
<meta property="og:type" content="article">
<meta property="og:title" content="webuploder+PHP实现大文件断点（切片）续传">
<meta property="og:url" content="http://example.com/2017/05/08/php/webuploder+PHP%E5%AE%9E%E7%8E%B0%E5%A4%A7%E6%96%87%E4%BB%B6%E6%96%AD%E7%82%B9%EF%BC%88%E5%88%87%E7%89%87%EF%BC%89%E7%BB%AD%E4%BC%A0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="前言做手机游戏运营难免会遇到上传超大APP，但受限于宽带、http响应时间等因素，导致文件上传不成功。在网上搜罗了一圈，终于在github上找到了kazaff大神对于webuploader大文件上传的详细介绍。 下面整理一下自己的实现过程吧（kazaff已做了详细介绍，这里只是借鉴一下）：文件目录结构： index.html1234567891011121314151617181920212223">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/images/webuploader/webuploader_php_menu.png">
<meta property="og:image" content="http://example.com/images/webuploader/index_html.png">
<meta property="article:published_time" content="2017-05-08T03:18:07.000Z">
<meta property="article:modified_time" content="2022-02-10T03:11:25.708Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="PHP">
<meta property="article:tag" content="webuploader">
<meta property="article:tag" content="js">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="http://example.com/images/webuploader/webuploader_php_menu.png">
  
  
  <title>webuploder+PHP实现大文件断点（切片）续传 - Hexo</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.13","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>LZDF</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="webuploder+PHP实现大文件断点（切片）续传">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2017-05-08 11:18" pubdate>
        2017年5月8日 上午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      23k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      189 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">webuploder+PHP实现大文件断点（切片）续传</h1>
            
            <div class="markdown-body">
              <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>做手机游戏运营难免会遇到上传超大APP，但受限于宽带、http响应时间等因素，导致文件上传不成功。在网上搜罗了一圈，终于在github上找到了<a target="_blank" rel="noopener" href="https://github.com/kazaff">kazaff</a>大神对于<code>webuploader</code><a target="_blank" rel="noopener" href="http://blog.kazaff.me/2014/11/14/%E8%81%8A%E8%81%8A%E5%A4%A7%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/">大文件上传</a>的详细介绍。</p>
<p><em>下面整理一下自己的实现过程吧（kazaff已做了详细介绍，这里只是借鉴一下）：</em><br>文件目录结构：<br><img src='/images/webuploader/webuploader_php_menu.png' /></p>
<h1 id="index-html"><a href="#index-html" class="headerlink" title="index.html"></a><code>index.html</code></h1><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span><br><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>WebUploader<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/jquery-1.11.2.min.js&quot;</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/webuploader.js&quot;</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/md5.js&quot;</span>  <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">&quot;stylesheet&quot;</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;css/webuploader.css&quot;</span> /&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">style</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/css&quot;</span>&gt;</span><span class="css"></span><br><span class="css">            <span class="hljs-selector-class">.itemDel</span>, <span class="hljs-selector-class">.itemStop</span>, <span class="hljs-selector-class">.itemUpload</span>&#123;</span><br><span class="css">                <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">15px</span>;</span><br><span class="css">                <span class="hljs-attribute">color</span>: blue;</span><br><span class="css">                <span class="hljs-attribute">cursor</span>: pointer;</span><br><span class="css">            &#125;</span><br><span class="css">            <span class="hljs-selector-id">#theList</span>&#123;</span><br><span class="css">                <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>;</span><br><span class="css">                <span class="hljs-attribute">min-height</span>: <span class="hljs-number">100px</span>;</span><br><span class="css">                <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid red;</span><br><span class="css">            &#125;</span><br><span class="css">            <span class="hljs-selector-id">#theList</span> <span class="hljs-selector-class">.itemStop</span>&#123;</span><br><span class="css">                <span class="hljs-attribute">display</span>: none;</span><br><span class="css">            &#125;</span><br><span class="css">        </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;uploader&quot;</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;theList&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span><br>            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;picker&quot;</span>&gt;</span>选择文件<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span><br>        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">style</span>=<span class="hljs-string">&quot;color:red&quot;</span>&gt;</span>已实现大文件分段，FTP上传需要连接ftp服务器。<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span><br>    <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span><br>    <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span> <span class="hljs-attr">src</span>=<span class="hljs-string">&quot;js/bigFileUploader.js&quot;</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">&quot;utf-8&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span><br><span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span><br></code></pre></td></tr></table></figure>
<img src='/images/webuploader/index_html.png' />

<h1 id="bigFileUploader-js"><a href="#bigFileUploader-js" class="headerlink" title="bigFileUploader.js"></a><code>bigFileUploader.js</code></h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">var</span> userInfo = &#123;<span class="hljs-attr">userId</span>:<span class="hljs-string">&quot;up_file&quot;</span>, <span class="hljs-attr">md5</span>:<span class="hljs-string">&quot;&quot;</span>&#125;;   <span class="hljs-comment">//用户会话信息</span><br><span class="hljs-keyword">var</span> chunkSize =  <span class="hljs-number">2</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>;        <span class="hljs-comment">//分块大小</span><br><span class="hljs-keyword">var</span> uniqueFileName = <span class="hljs-literal">null</span>;          <span class="hljs-comment">//文件唯一标识符</span><br><span class="hljs-keyword">var</span> md5Mark = <span class="hljs-literal">null</span>;<br><br><span class="hljs-keyword">var</span> backEndUrl = <span class="hljs-string">&#x27;serverPHP/upload.php&#x27;</span>;<br><br>WebUploader.Uploader.register(&#123;<br>    <span class="hljs-string">&quot;before-send-file&quot;</span>: <span class="hljs-string">&quot;beforeSendFile&quot;</span><br>    , <span class="hljs-string">&quot;before-send&quot;</span>: <span class="hljs-string">&quot;beforeSend&quot;</span><br>    , <span class="hljs-string">&quot;after-send-file&quot;</span>: <span class="hljs-string">&quot;afterSendFile&quot;</span><br>&#125;, &#123;<br>    <span class="hljs-attr">beforeSendFile</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">file</span>) </span>&#123;<br>        <span class="hljs-comment">//秒传验证</span><br>        <span class="hljs-keyword">var</span> task = <span class="hljs-keyword">new</span> $.Deferred();<br>        <span class="hljs-keyword">var</span> start = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime();<br>        (<span class="hljs-keyword">new</span> WebUploader.Uploader()).md5File(file, <span class="hljs-number">0</span>, <span class="hljs-number">10</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>).progress(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">percentage</span>) </span>&#123;<br>            <span class="hljs-built_in">console</span>.log(percentage);<br>        &#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">val</span>) </span>&#123;<br>            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;总耗时: &quot;</span> + ((<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime()) - start) / <span class="hljs-number">1000</span>);<br><br>            md5Mark = val;<br>            userInfo.md5 = val;<br><br>            $.ajax(&#123;<br>                <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;POST&quot;</span>,<br>                <span class="hljs-attr">url</span>: backEndUrl,<br>                <span class="hljs-attr">data</span>: &#123;<br>                    <span class="hljs-attr">status</span>: <span class="hljs-string">&quot;md5Check&quot;</span>,<br>                    <span class="hljs-attr">md5</span>: val<br>                &#125;,<br>                <span class="hljs-attr">cache</span>: <span class="hljs-literal">false</span>,<br>                <span class="hljs-attr">timeout</span>: <span class="hljs-number">1000</span>, <span class="hljs-comment">//todo 超时的话，只能认为该文件不曾上传过</span><br>                <span class="hljs-attr">dataType</span>: <span class="hljs-string">&quot;json&quot;</span><br>            &#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data, textStatus, jqXHR</span>) </span>&#123;<br>                <span class="hljs-built_in">console</span>.log(data, <span class="hljs-number">1111</span>);<br>                <span class="hljs-keyword">if</span> (data.ifExist) &#123;   <span class="hljs-comment">//若存在，这返回失败给WebUploader，表明该文件不需要上传</span><br>                    task.reject();<br><br>                    uploader.skipFile(file);<br>                    file.path = data.path;<br>                    UploadComlate(file);<br>                &#125; <span class="hljs-keyword">else</span> &#123;<br>                    task.resolve();<br>                    <span class="hljs-comment">//拿到上传文件的唯一名称，用于断点续传</span><br>                    uniqueFileName = md5(<span class="hljs-string">&#x27;&#x27;</span> + userInfo.userId + file.name + file.type + file.lastModifiedDate + file.size);<br>                &#125;<br>            &#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">jqXHR, textStatus, errorThrown</span>) </span>&#123;    <span class="hljs-comment">//任何形式的验证失败，都触发重新上传</span><br>                task.resolve();<br>                <span class="hljs-comment">//拿到上传文件的唯一名称，用于断点续传</span><br>                uniqueFileName = md5(<span class="hljs-string">&#x27;&#x27;</span> + userInfo.userId + file.name + file.type + file.lastModifiedDate + file.size);<br>            &#125;);<br>        &#125;);<br>        <span class="hljs-keyword">return</span> $.when(task);<br>    &#125;,<br>    <span class="hljs-attr">beforeSend</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">block</span>) </span>&#123;<br>        <span class="hljs-comment">//分片验证是否已传过，用于断点续传</span><br>        <span class="hljs-keyword">var</span> task = <span class="hljs-keyword">new</span> $.Deferred();<br>        $.ajax(&#123;<br>            <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;POST&quot;</span>,<br>            <span class="hljs-attr">url</span>: backEndUrl,<br>            <span class="hljs-attr">data</span>: &#123;<br>                <span class="hljs-attr">status</span>: <span class="hljs-string">&quot;chunkCheck&quot;</span>,<br>                <span class="hljs-attr">name</span>: uniqueFileName,<br>                <span class="hljs-attr">chunkIndex</span>: block.chunk,<br>                <span class="hljs-attr">size</span>: block.end - block.start<br>            &#125;,<br>            <span class="hljs-attr">cache</span>: <span class="hljs-literal">false</span>,<br>            <span class="hljs-attr">timeout</span>: <span class="hljs-number">1000</span>, <span class="hljs-comment">//todo 超时的话，只能认为该分片未上传过</span><br>            <span class="hljs-attr">dataType</span>: <span class="hljs-string">&quot;json&quot;</span><br>        &#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data, textStatus, jqXHR</span>) </span>&#123;<br>            <span class="hljs-keyword">if</span> (data.ifExist) &#123;   <span class="hljs-comment">//若存在，返回失败给WebUploader，表明该分块不需要上传</span><br>                task.reject();<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                task.resolve();<br>            &#125;<br>        &#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">jqXHR, textStatus, errorThrown</span>) </span>&#123;    <span class="hljs-comment">//任何形式的验证失败，都触发重新上传</span><br>            task.resolve();<br>        &#125;);<br><br>        <span class="hljs-keyword">return</span> $.when(task);<br>    &#125;,<br>    <span class="hljs-attr">afterSendFile</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">file</span>) </span>&#123;<br>        <span class="hljs-keyword">var</span> chunksTotal = <span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">if</span> ((chunksTotal = <span class="hljs-built_in">Math</span>.ceil(file.size / chunkSize)) &gt; <span class="hljs-number">1</span>) &#123;<br>            <span class="hljs-comment">//合并请求</span><br>            <span class="hljs-keyword">var</span> task = <span class="hljs-keyword">new</span> $.Deferred();<br>            $.ajax(&#123;<br>                <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;POST&quot;</span>,<br>                <span class="hljs-attr">url</span>: backEndUrl,<br>                <span class="hljs-attr">data</span>: &#123;<br>                    <span class="hljs-attr">status</span>: <span class="hljs-string">&quot;chunksMerge&quot;</span>,<br>                    <span class="hljs-attr">name</span>: uniqueFileName,<br>                    <span class="hljs-attr">chunks</span>: chunksTotal,<br>                    <span class="hljs-attr">ext</span>: file.ext,<br>                    <span class="hljs-attr">md5</span>: md5Mark<br>                &#125;,<br>                <span class="hljs-attr">cache</span>: <span class="hljs-literal">false</span>,<br>                <span class="hljs-attr">dataType</span>: <span class="hljs-string">&quot;json&quot;</span><br>            &#125;).then(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data, textStatus, jqXHR</span>) </span>&#123;<br><br>                <span class="hljs-comment">//todo 检查响应是否正常</span><br>                <span class="hljs-comment">//console.log(data);</span><br>                task.resolve();<br>                file.path = data.path;<br>                UploadComlate(file, data);<br><br>            &#125;, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">jqXHR, textStatus, errorThrown</span>) </span>&#123;<br>                task.reject();<br>            &#125;);<br><br>            <span class="hljs-keyword">return</span> $.when(task);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            UploadComlate(file);<br>        &#125;<br>    &#125;<br>&#125;);<br><br><span class="hljs-keyword">var</span> uploader = WebUploader.create(&#123;<br>    <span class="hljs-attr">swf</span>: <span class="hljs-string">&quot;Uploader.swf&quot;</span>,<br>    <span class="hljs-attr">server</span>: backEndUrl,<br>    <span class="hljs-attr">pick</span>: <span class="hljs-string">&quot;#picker&quot;</span>,<br>    <span class="hljs-attr">resize</span>: <span class="hljs-literal">false</span>,<br>    <span class="hljs-attr">dnd</span>: <span class="hljs-string">&quot;#theList&quot;</span>,<br>    <span class="hljs-attr">paste</span>: <span class="hljs-built_in">document</span>.body,<br>    <span class="hljs-attr">disableGlobalDnd</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">thumb</span>: &#123;<br>        <span class="hljs-attr">width</span>: <span class="hljs-number">100</span>,<br>        <span class="hljs-attr">height</span>: <span class="hljs-number">100</span>,<br>        <span class="hljs-attr">quality</span>: <span class="hljs-number">70</span>,<br>        <span class="hljs-attr">allowMagnify</span>: <span class="hljs-literal">true</span>,<br>        <span class="hljs-attr">crop</span>: <span class="hljs-literal">true</span><br>        <span class="hljs-comment">//, type: &quot;image/jpeg&quot;</span><br>    &#125;,<br>    <span class="hljs-comment">/*, compress: &#123;</span><br><span class="hljs-comment">     quality: 90</span><br><span class="hljs-comment">     , allowMagnify: false</span><br><span class="hljs-comment">     , crop: false</span><br><span class="hljs-comment">     , preserveHeaders: true</span><br><span class="hljs-comment">     , noCompressIfLarger: true</span><br><span class="hljs-comment">     ,compressSize: 100000</span><br><span class="hljs-comment">     &#125;,*/</span><br>    <span class="hljs-attr">compress</span>: <span class="hljs-literal">false</span>,<br>    <span class="hljs-attr">prepareNextFile</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">chunked</span>: <span class="hljs-literal">true</span>,<br>    <span class="hljs-attr">chunkSize</span>: chunkSize,<br>    <span class="hljs-attr">formData</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>)</span>&#123;<br>        <span class="hljs-keyword">return</span> $.extend(<span class="hljs-literal">true</span>, &#123; &#125;, userInfo);<br>    &#125;,<br>    <span class="hljs-attr">threads</span>:<span class="hljs-number">1</span>,<br>    <span class="hljs-attr">fileNumLimit</span>: <span class="hljs-number">1</span>,<br>    <span class="hljs-attr">fileSingleSizeLimit</span>: <span class="hljs-number">1000</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>,<br>    <span class="hljs-attr">duplicate</span>: <span class="hljs-literal">true</span><br>&#125;);<br><br>uploader.on(<span class="hljs-string">&quot;fileQueued&quot;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">file</span>) </span>&#123;<br><br>    $(<span class="hljs-string">&quot;#theList&quot;</span>).append(<span class="hljs-string">&#x27;&lt;li id=&quot;&#x27;</span> + file.id + <span class="hljs-string">&#x27;&quot;&gt;&#x27;</span> +<br>        <span class="hljs-string">&#x27;&lt;img /&gt;&lt;span&gt;&#x27;</span> + file.name + <span class="hljs-string">&#x27;&lt;/span&gt;&lt;span class=&quot;itemUpload&quot;&gt;上传&lt;/span&gt;&lt;span class=&quot;itemStop&quot;&gt;暂停&lt;/span&gt;&lt;span class=&quot;itemDel&quot;&gt;删除&lt;/span&gt;&#x27;</span> +<br>        <span class="hljs-string">&#x27;&lt;div class=&quot;percentage&quot;&gt;&lt;/div&gt;&#x27;</span> +<br>        <span class="hljs-string">&#x27;&lt;/li&gt;&#x27;</span>);<br><br>    <span class="hljs-keyword">var</span> $img = $(<span class="hljs-string">&quot;#&quot;</span> + file.id).find(<span class="hljs-string">&quot;img&quot;</span>);<br><br>     uploader.makeThumb(file, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">error, src</span>) </span>&#123;<br>     <span class="hljs-keyword">if</span> (error) &#123;<br>     $img.replaceWith(<span class="hljs-string">&quot;&lt;span&gt;不能预览&lt;/span&gt;&quot;</span>);<br>     &#125;<br><br>     $img.attr(<span class="hljs-string">&quot;src&quot;</span>, src);<br>     &#125;);<br><br>&#125;);<br><br>$(<span class="hljs-string">&quot;#theList&quot;</span>).on(<span class="hljs-string">&quot;click&quot;</span>, <span class="hljs-string">&quot;.itemUpload&quot;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    uploader.upload();<br>    <span class="hljs-comment">//&quot;上传&quot;--&gt;&quot;暂停&quot;</span><br>    $(<span class="hljs-built_in">this</span>).hide();<br>    $(<span class="hljs-string">&quot;.itemStop&quot;</span>).show();<br>&#125;);<br><br>$(<span class="hljs-string">&quot;#theList&quot;</span>).on(<span class="hljs-string">&quot;click&quot;</span>, <span class="hljs-string">&quot;.itemStop&quot;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    uploader.stop(<span class="hljs-literal">true</span>);<br>    <span class="hljs-comment">//&quot;暂停&quot;--&gt;&quot;上传&quot;</span><br>    $(<span class="hljs-built_in">this</span>).hide();<br>    $(<span class="hljs-string">&quot;.itemUpload&quot;</span>).show();<br>&#125;);<br><br><span class="hljs-comment">//todo 如果要删除的文件正在上传（包括暂停），则需要发送给后端一个请求用来清除服务器端的缓存文件</span><br>$(<span class="hljs-string">&quot;#theList&quot;</span>).on(<span class="hljs-string">&quot;click&quot;</span>, <span class="hljs-string">&quot;.itemDel&quot;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>    uploader.removeFile($(<span class="hljs-built_in">this</span>).parent().attr(<span class="hljs-string">&quot;id&quot;</span>));   <span class="hljs-comment">//从上传文件列表中删除</span><br>    $(<span class="hljs-built_in">this</span>).parent().remove();  <span class="hljs-comment">//从上传列表dom中删除</span><br>&#125;);<br><br>uploader.on(<span class="hljs-string">&quot;uploadProgress&quot;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">file, percentage</span>) </span>&#123;<br>    $(<span class="hljs-string">&quot;#&quot;</span> + file.id + <span class="hljs-string">&quot; .percentage&quot;</span>).text(percentage * <span class="hljs-number">100</span> + <span class="hljs-string">&quot;%&quot;</span>);<br>&#125;);<br><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">UploadComlate</span>(<span class="hljs-params">file, data</span>) </span>&#123;<br>    <span class="hljs-built_in">console</span>.log(file, data);<br>    <span class="hljs-keyword">if</span>(file &amp;&amp; data &amp;&amp; data.type == <span class="hljs-number">1</span>)&#123;<br>        $.ajax(&#123;<br>            <span class="hljs-attr">url</span>: backEndUrl,<br>            <span class="hljs-attr">type</span>: <span class="hljs-string">&quot;POST&quot;</span>,<br>            <span class="hljs-attr">dataType</span>: <span class="hljs-string">&quot;json&quot;</span>,<br>            <span class="hljs-attr">cache</span>: <span class="hljs-literal">false</span>,<br>            <span class="hljs-attr">data</span>:&#123;<br>                <span class="hljs-attr">act</span>: <span class="hljs-string">&quot;upload&quot;</span>,<br>                <span class="hljs-attr">real_name</span>: file.name,<br>                <span class="hljs-attr">tmp_name</span>: data.path,<br>                <span class="hljs-attr">code</span>: data.type,<br>                <span class="hljs-attr">size</span>: file.size<br>            &#125;,<br>            <span class="hljs-attr">success</span>:<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>&#123;<br>                <span class="hljs-keyword">if</span>(data.code == <span class="hljs-number">1</span>)&#123;<br>                    <span class="hljs-comment">/* $(&quot;#app_url&quot;).val(data.url);</span><br><span class="hljs-comment">                    $(&quot;#app_size&quot;).val(data.size); */</span><br><br>                    $(<span class="hljs-string">&quot;#&quot;</span> + file.id + <span class="hljs-string">&quot; .percentage&quot;</span>).text(<span class="hljs-string">&quot;上传完毕&quot;</span>).css(&#123;<span class="hljs-string">&#x27;color&#x27;</span>:<span class="hljs-string">&#x27;red&#x27;</span>&#125;);<br>                    $(<span class="hljs-string">&quot;.itemStop&quot;</span>).hide();<br>                    $(<span class="hljs-string">&quot;.itemUpload&quot;</span>).hide();<br>                    $(<span class="hljs-string">&quot;.itemDel&quot;</span>).hide();<br>                &#125;<br>                <span class="hljs-keyword">if</span>(data.code == <span class="hljs-number">2</span>)&#123;<br>                    $(<span class="hljs-string">&quot;#&quot;</span> + file.id + <span class="hljs-string">&quot; .percentage&quot;</span>).text(data.mes).css(&#123;<span class="hljs-string">&#x27;color&#x27;</span>:<span class="hljs-string">&#x27;red&#x27;</span>&#125;);<br>                &#125;<br>            &#125;,<br>            <span class="hljs-attr">error</span>:<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>&#123;<br>                $(<span class="hljs-string">&quot;#&quot;</span> + file.id + <span class="hljs-string">&quot; .percentage&quot;</span>).text(<span class="hljs-string">&#x27;网络错误！&#x27;</span>).css(&#123;<span class="hljs-string">&#x27;color&#x27;</span>:<span class="hljs-string">&#x27;red&#x27;</span>&#125;);<br>            &#125;<br>        &#125;);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h1 id="upload-php"><a href="#upload-php" class="headerlink" title="upload.php"></a><code>upload.php</code></h1><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-meta">&lt;?php</span><br>@define(<span class="hljs-string">&#x27;ROOT&#x27;</span>, dirname(dirname(<span class="hljs-keyword">__FILE__</span>)));<br><span class="hljs-keyword">include_once</span>(<span class="hljs-string">&#x27;./FileUpload.php&#x27;</span>);<br><span class="hljs-keyword">include_once</span>(<span class="hljs-string">&#x27;./Ftp.php&#x27;</span>);<br><br><br><span class="hljs-keyword">if</span>(@<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;act&#x27;</span>] == <span class="hljs-string">&#x27;upload&#x27;</span>)&#123;<br>    <span class="hljs-variable">$data</span> = <span class="hljs-variable">$_POST</span>;<br>    <span class="hljs-variable">$arr</span> = uploadRunApk(<span class="hljs-variable">$data</span>, <span class="hljs-string">&#x27;upload/&#x27;</span> . date(<span class="hljs-string">&quot;Y&quot;</span>) . <span class="hljs-string">&#x27;/&#x27;</span> . date(<span class="hljs-string">&#x27;m&#x27;</span>) . <span class="hljs-string">&#x27;/&#x27;</span>);<br>    <span class="hljs-keyword">echo</span> json_encode(<span class="hljs-variable">$arr</span>);<br>    <span class="hljs-keyword">exit</span>;<br>&#125;<br><br><span class="hljs-variable">$arr</span> = uploadChunked();<br><span class="hljs-keyword">echo</span> json_encode(<span class="hljs-variable">$arr</span>);<br><span class="hljs-keyword">exit</span>;<br><br><span class="hljs-comment">//通过FTP上传到指定位置(这里以上传apk文件为例)</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">uploadRunApk</span>(<span class="hljs-params"><span class="hljs-variable">$file</span>, <span class="hljs-variable">$save_path</span></span>)</span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-keyword">global</span> <span class="hljs-variable">$_SC</span>;<br>    <span class="hljs-variable">$max_file_size_in_bytes</span> = <span class="hljs-number">1073741824</span>;                <span class="hljs-comment">// 1024M in bytes</span><br>    <span class="hljs-comment">//var_dump($file);exit;</span><br>    <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$file</span>)) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;找不到文件&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$file</span>[<span class="hljs-string">&quot;tmp_name&quot;</span>])) &#123;     <span class="hljs-comment">//is_uploaded_file()</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件无法上传&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$file</span>[<span class="hljs-string">&#x27;real_name&#x27;</span>])) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件名不存在&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-comment">//$file_size = @filesize($file[&quot;tmp_name&quot;]);</span><br>    <span class="hljs-variable">$file_size</span> = <span class="hljs-variable">$file</span>[<span class="hljs-string">&#x27;size&#x27;</span>];<br>    <span class="hljs-keyword">if</span> (!<span class="hljs-variable">$file_size</span> || <span class="hljs-variable">$file_size</span> &gt; <span class="hljs-variable">$max_file_size_in_bytes</span>) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件size太大&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-keyword">if</span> (<span class="hljs-variable">$file_size</span> &lt;= <span class="hljs-number">0</span>) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件大小不能为0&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-variable">$path_info</span> = pathinfo(<span class="hljs-variable">$file</span>[<span class="hljs-string">&#x27;real_name&#x27;</span>]);<br>    <span class="hljs-variable">$file_extension</span> = <span class="hljs-variable">$path_info</span>[<span class="hljs-string">&quot;extension&quot;</span>];<br><br>    <span class="hljs-keyword">if</span>(<span class="hljs-variable">$file_extension</span> != <span class="hljs-string">&#x27;apk&#x27;</span>)&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件类型错误:&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-comment">//$fmd5 = md5_file($file[&quot;tmp_name&quot;]);</span><br>    <span class="hljs-comment">//$file_name = $save_path . $fmd5 . &#x27;.&#x27; . $file_extension;</span><br>    <span class="hljs-variable">$file_name</span> = <span class="hljs-variable">$save_path</span> . <span class="hljs-variable">$path_info</span>[<span class="hljs-string">&#x27;filename&#x27;</span>] . <span class="hljs-string">&#x27;.&#x27;</span> .<span class="hljs-variable">$file_extension</span>;<br>    <span class="hljs-variable">$ftp</span> = <span class="hljs-keyword">new</span> Ftp(<span class="hljs-variable">$ftp_host</span>, <span class="hljs-variable">$ftp_port</span>, <span class="hljs-variable">$ftp_user</span>, <span class="hljs-variable">$ftp_pass</span>);<br>    <span class="hljs-keyword">if</span> (<span class="hljs-variable">$ftp</span>-&gt;up_file(<span class="hljs-variable">$file</span>[<span class="hljs-string">&quot;tmp_name&quot;</span>], <span class="hljs-variable">$file_name</span>)) &#123;<br>        <span class="hljs-variable">$show_name</span> = substr(<span class="hljs-variable">$file_name</span>, strrpos(<span class="hljs-variable">$file_name</span>, <span class="hljs-string">&#x27;/&#x27;</span>) + <span class="hljs-number">1</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-string">&#x27;文件上传成功&#x27;</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;url&#x27;</span> =&gt; <span class="hljs-variable">$file_name</span>, <span class="hljs-string">&#x27;size&#x27;</span> =&gt; <span class="hljs-variable">$file_size</span>, <span class="hljs-string">&#x27;show_name&#x27;</span> =&gt; <span class="hljs-variable">$show_name</span>);<br>    &#125; <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;mes&#x27;</span> =&gt; <span class="hljs-variable">$file_name</span>, <span class="hljs-string">&#x27;code&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">uploadChunked</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">//关闭缓存</span><br>    header(<span class="hljs-string">&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;</span>);<br>    header(<span class="hljs-string">&quot;Last-Modified: &quot;</span> . gmdate(<span class="hljs-string">&quot;D, d M Y H:i:s&quot;</span>) . <span class="hljs-string">&quot; GMT&quot;</span>);<br>    header(<span class="hljs-string">&quot;Cache-Control: no-store, no-cache, must-revalidate&quot;</span>);<br>    header(<span class="hljs-string">&quot;Cache-Control: post-check=0, pre-check=0&quot;</span>, <span class="hljs-literal">false</span>);<br>    header(<span class="hljs-string">&quot;Pragma: no-cache&quot;</span>);<br><br>    <span class="hljs-keyword">global</span> <span class="hljs-variable">$_SC</span>;<br>    <span class="hljs-variable">$uploader</span> = <span class="hljs-keyword">new</span> FileUpload(<span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;apk&#x27;</span>, <span class="hljs-string">&#x27;jpg&#x27;</span>, <span class="hljs-string">&#x27;png&#x27;</span>, <span class="hljs-string">&#x27;zip&#x27;</span>));<br><br>    <span class="hljs-comment">//用于断点续传，验证指定分块是否已经存在，避免重复上传</span><br>    <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;status&#x27;</span>])) &#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;status&#x27;</span>] == <span class="hljs-string">&#x27;chunkCheck&#x27;</span>) &#123;<br>            <span class="hljs-variable">$target</span> = <span class="hljs-variable">$uploader</span>-&gt;path . <span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;name&#x27;</span>] . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;chunkIndex&#x27;</span>];<br>            <span class="hljs-keyword">if</span> (file_exists(<span class="hljs-variable">$target</span>) &amp;&amp; filesize(<span class="hljs-variable">$target</span>) == <span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;size&#x27;</span>]) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;ifExist&#x27;</span> =&gt; <span class="hljs-number">1</span>);<br>            &#125;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;ifExist&#x27;</span> =&gt; <span class="hljs-number">0</span>);<br><br>        &#125; <span class="hljs-keyword">elseif</span> (<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;status&#x27;</span>] == <span class="hljs-string">&#x27;md5Check&#x27;</span>) &#123;<br>            <span class="hljs-comment">//todo 模拟持久层查询</span><br>            <span class="hljs-variable">$dataArr</span> = <span class="hljs-keyword">array</span>(<br>                <span class="hljs-string">&#x27;b0201e4d41b2eeefc7d3d355a44c6f5a&#x27;</span> =&gt; <span class="hljs-string">&#x27;kazaff2.jpg&#x27;</span><br>            );<br><br>            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$dataArr</span>[<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;md5&#x27;</span>]])) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;ifExist&#x27;</span> =&gt; <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;path&#x27;</span> =&gt; <span class="hljs-variable">$dataArr</span>[<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;md5&#x27;</span>]]);<br>            &#125;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;ifExist&#x27;</span> =&gt; <span class="hljs-number">0</span>);<br>        &#125; <span class="hljs-keyword">elseif</span> (<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;status&#x27;</span>] == <span class="hljs-string">&#x27;chunksMerge&#x27;</span>) &#123;<br><br>            <span class="hljs-keyword">if</span> (<span class="hljs-variable">$path</span> = <span class="hljs-variable">$uploader</span>-&gt;chunksMerge(<span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;name&#x27;</span>], <span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;chunks&#x27;</span>], <span class="hljs-variable">$_POST</span>[<span class="hljs-string">&#x27;ext&#x27;</span>])) &#123;<br>                <span class="hljs-comment">//todo 把md5签名存入持久层，供未来的秒传验证</span><br>                <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;status&#x27;</span> =&gt; <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;path&#x27;</span> =&gt; <span class="hljs-variable">$path</span>, <span class="hljs-string">&#x27;type&#x27;</span> =&gt; <span class="hljs-number">1</span>);<br>            &#125;<br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;status&#x27;</span> =&gt; <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;chunksMerge&#x27;</span> =&gt; <span class="hljs-number">0</span>);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-keyword">if</span> ((<span class="hljs-variable">$path</span> = <span class="hljs-variable">$uploader</span>-&gt;upload(<span class="hljs-string">&#x27;file&#x27;</span>, <span class="hljs-variable">$_POST</span>)) !== <span class="hljs-literal">false</span>) &#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;status&#x27;</span> =&gt; <span class="hljs-number">1</span>, <span class="hljs-string">&#x27;path&#x27;</span> =&gt; <span class="hljs-variable">$path</span>, <span class="hljs-string">&#x27;type&#x27;</span> =&gt; <span class="hljs-number">2</span>);<br>    &#125;<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">array</span>(<span class="hljs-string">&#x27;status&#x27;</span> =&gt; <span class="hljs-number">0</span>, <span class="hljs-string">&#x27;upload&#x27;</span> =&gt; <span class="hljs-number">0</span>);<br>&#125;<br><br></code></pre></td></tr></table></figure>

<h1 id="FileUpload-php"><a href="#FileUpload-php" class="headerlink" title="FileUpload.php"></a><code>FileUpload.php</code></h1><figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br></pre></td><td class="code"><pre><code class="hljs php"><span class="hljs-meta">&lt;?php</span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> *  PHP通用文件上传类</span><br><span class="hljs-comment"> *</span><br><span class="hljs-comment"> *  支持单文件和多文件上传</span><br><span class="hljs-comment"> */</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">FileUpload</span></span><br><span class="hljs-class"></span>&#123;<br>    <span class="hljs-comment">//要配置的内容</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-variable">$path</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$allowtype</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$maxsize</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$israndname</span>;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$originName</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$tmpFileName</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$fileType</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$fileSize</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$newFileName</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$errorNum</span> = <span class="hljs-number">0</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$errorMess</span> = <span class="hljs-string">&quot;&quot;</span>;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$isChunk</span> = <span class="hljs-literal">false</span>;<br>    <span class="hljs-keyword">private</span> <span class="hljs-variable">$indexOfChunk</span> = <span class="hljs-number">0</span>;<br><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params"><span class="hljs-variable">$type</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;path = ROOT . <span class="hljs-string">&#x27;/upload&#x27;</span>;<br>        <span class="hljs-comment">//$this-&gt;allowtype = array(&#x27;jpg&#x27;, &#x27;jpeg&#x27;, &#x27;gif&#x27;, &#x27;png&#x27;, &#x27;mp4&#x27;, &#x27;mp3&#x27;, &#x27;zip&#x27;, &#x27;apk&#x27;, &#x27;pdf&#x27;, &#x27;rar&#x27;);</span><br>        <span class="hljs-keyword">$this</span>-&gt;allowtype = <span class="hljs-variable">$type</span>;<br>        <span class="hljs-keyword">$this</span>-&gt;maxsize = <span class="hljs-number">1073741824</span>;                <span class="hljs-comment">// 1024M in bytes</span><br>        <span class="hljs-keyword">$this</span>-&gt;israndname = <span class="hljs-literal">true</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * 用于设置成员属性($path, $allowtype, $maxsize, $israndname)</span><br><span class="hljs-comment">     * 可以通过连贯操作一次设置多个属性值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $key  成员属性（不区分大小写）</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $val  为成员属性设置的值</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> object 返回自己对象$this, 可以用于连贯操作</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">set</span>(<span class="hljs-params"><span class="hljs-variable">$key</span>, <span class="hljs-variable">$val</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-variable">$key</span> = strtolower(<span class="hljs-variable">$key</span>);<br>        <span class="hljs-keyword">if</span> (array_key_exists(<span class="hljs-variable">$key</span>, get_class_vars(get_class(<span class="hljs-keyword">$this</span>)))) &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-variable">$key</span>, <span class="hljs-variable">$val</span>);<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//为单个成员属性设置值</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setOption</span>(<span class="hljs-params"><span class="hljs-variable">$key</span>, <span class="hljs-variable">$val</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;<span class="hljs-variable">$key</span> = <span class="hljs-variable">$val</span>;<br>        <span class="hljs-keyword">return</span> <span class="hljs-variable">$val</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/** 调用该方法上传文件</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $fileField</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $info</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> bool</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@throws</span> Exception</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">upload</span>(<span class="hljs-params"><span class="hljs-variable">$fileField</span>, <span class="hljs-variable">$info</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-comment">//判断是否为分块上传</span><br>        <span class="hljs-keyword">$this</span>-&gt;checkChunk(<span class="hljs-variable">$info</span>);<br><br>        <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">$this</span>-&gt;checkFilePath(<span class="hljs-keyword">$this</span>-&gt;path))<br>        &#123;<br>            <span class="hljs-comment">//$this-&gt;errorMess = $this-&gt;getError();</span><br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125;<br><br>        <span class="hljs-comment">//将文件上传的信息取出赋给变量</span><br>        <span class="hljs-variable">$name</span> = <span class="hljs-variable">$_FILES</span>[<span class="hljs-variable">$fileField</span>][<span class="hljs-string">&#x27;name&#x27;</span>];<br>        <span class="hljs-variable">$tmp_name</span> = <span class="hljs-variable">$_FILES</span>[<span class="hljs-variable">$fileField</span>][<span class="hljs-string">&#x27;tmp_name&#x27;</span>];<br>        <span class="hljs-variable">$size</span> = <span class="hljs-variable">$_FILES</span>[<span class="hljs-variable">$fileField</span>][<span class="hljs-string">&#x27;size&#x27;</span>];<br>        <span class="hljs-variable">$error</span> = <span class="hljs-variable">$_FILES</span>[<span class="hljs-variable">$fileField</span>][<span class="hljs-string">&#x27;error&#x27;</span>];<br><br>        <span class="hljs-comment">//设置文件信息</span><br>        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;setFiles(<span class="hljs-variable">$name</span>, <span class="hljs-variable">$tmp_name</span>, <span class="hljs-variable">$size</span>, <span class="hljs-variable">$error</span>))<br>        &#123;<br>            <span class="hljs-comment">//如果是分块，则创建一个唯一名称的文件夹用来保存该文件的所有分块</span><br>            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;isChunk) &#123;<br>                <span class="hljs-variable">$uploadDir</span> = <span class="hljs-keyword">$this</span>-&gt;path;<br>                <span class="hljs-variable">$tmpName</span> = <span class="hljs-keyword">$this</span>-&gt;setDirNameForChunks(<span class="hljs-variable">$info</span>);<br>                <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">$this</span>-&gt;checkFilePath(<span class="hljs-variable">$uploadDir</span> . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$tmpName</span>))<br>                &#123;<br>                    <span class="hljs-comment">//$this-&gt;errorMess = $this-&gt;getError();</span><br>                    <span class="hljs-comment">//return false;</span><br>                    show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>                &#125;<br>                <span class="hljs-comment">//创建一个对应的文件，用来记录上传分块文件的修改时间，用于清理长期未完成的垃圾分块</span><br>                touch(<span class="hljs-variable">$uploadDir</span> . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$tmpName</span> . <span class="hljs-string">&#x27;.tmp&#x27;</span>);<br>            &#125;<br><br>            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;checkFileSize() &amp;&amp; <span class="hljs-keyword">$this</span>-&gt;checkFileType()) &#123;<br>                <span class="hljs-keyword">$this</span>-&gt;setNewFileName();<br>                <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;copyFile()) &#123;<br>                    <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-keyword">$this</span>-&gt;newFileName;<br>                &#125;<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-comment">//$this-&gt;errorMess = $this-&gt;getError();</span><br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br><br>    <span class="hljs-comment">/**</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $uniqueFileName</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $chunksTotal</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@param</span> $fileExt</span><br><span class="hljs-comment">     * <span class="hljs-doctag">@return</span> bool</span><br><span class="hljs-comment">     */</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">chunksMerge</span>(<span class="hljs-params"><span class="hljs-variable">$uniqueFileName</span>, <span class="hljs-variable">$chunksTotal</span>, <span class="hljs-variable">$fileExt</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-variable">$targetDir</span> = <span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$uniqueFileName</span>;<br><br>        <span class="hljs-comment">//检查对应文件夹中的分块文件数量是否和总数保持一致</span><br>        <span class="hljs-keyword">if</span> (<span class="hljs-variable">$chunksTotal</span> &gt; <span class="hljs-number">1</span> &amp;&amp; (count(scandir(<span class="hljs-variable">$targetDir</span>)) - <span class="hljs-number">2</span>) == <span class="hljs-variable">$chunksTotal</span>)<br>        &#123;<br>            <span class="hljs-comment">//同步锁机制</span><br>            <span class="hljs-variable">$lockFd</span> = fopen(<span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$uniqueFileName</span> . <span class="hljs-string">&#x27;.lock&#x27;</span>, <span class="hljs-string">&quot;w&quot;</span>);<br>            <span class="hljs-keyword">if</span> (!flock(<span class="hljs-variable">$lockFd</span>, LOCK_EX | LOCK_NB))<br>            &#123;<br>                fclose(<span class="hljs-variable">$lockFd</span>);<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>            &#125;<br><br>            <span class="hljs-comment">//进行合并</span><br>            <span class="hljs-keyword">$this</span>-&gt;fileType = <span class="hljs-variable">$fileExt</span>;<br>            <span class="hljs-variable">$finalName</span> = <span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . (<span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;newFileName&#x27;</span>, <span class="hljs-keyword">$this</span>-&gt;proRandName()));<br>            <span class="hljs-variable">$file</span> = fopen(<span class="hljs-variable">$finalName</span>, <span class="hljs-string">&#x27;wb&#x27;</span>);<br><br>            <span class="hljs-keyword">for</span> (<span class="hljs-variable">$index</span> = <span class="hljs-number">0</span>; <span class="hljs-variable">$index</span> &lt; <span class="hljs-variable">$chunksTotal</span>; <span class="hljs-variable">$index</span>++)<br>            &#123;<br>                <span class="hljs-variable">$tmpFile</span> = <span class="hljs-variable">$targetDir</span> . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$index</span>;<br>                <span class="hljs-variable">$chunkFile</span> = fopen(<span class="hljs-variable">$tmpFile</span>, <span class="hljs-string">&#x27;rb&#x27;</span>);<br>                <span class="hljs-variable">$content</span> = fread(<span class="hljs-variable">$chunkFile</span>, filesize(<span class="hljs-variable">$tmpFile</span>));<br>                fclose(<span class="hljs-variable">$chunkFile</span>);<br>                fwrite(<span class="hljs-variable">$file</span>, <span class="hljs-variable">$content</span>);<br><br>                <span class="hljs-comment">//删除chunk文件</span><br>                unlink(<span class="hljs-variable">$tmpFile</span>);<br>            &#125;<br><br>            fclose(<span class="hljs-variable">$file</span>);<br><br>            <span class="hljs-comment">//删除chunk文件夹</span><br>            rmdir(<span class="hljs-variable">$targetDir</span>);<br>            unlink(<span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$uniqueFileName</span> . <span class="hljs-string">&#x27;.tmp&#x27;</span>);<br><br>            <span class="hljs-comment">//解锁</span><br>            flock(<span class="hljs-variable">$lockFd</span>, LOCK_UN);<br>            fclose(<span class="hljs-variable">$lockFd</span>);<br>            unlink(<span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-variable">$uniqueFileName</span> . <span class="hljs-string">&#x27;.lock&#x27;</span>);<br><br>            <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;path . <span class="hljs-string">&#x27;/&#x27;</span> . <span class="hljs-keyword">$this</span>-&gt;newFileName;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//获取上传后的文件名称</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getFileName</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">$this</span>-&gt;newFileName;<br>    &#125;<br><br>    <span class="hljs-comment">//上传失败后，调用该方法则返回，上传出错信息</span><br>    <span class="hljs-comment">/*public function getErrorMsg()</span><br><span class="hljs-comment">    &#123;</span><br><span class="hljs-comment">        return $this-&gt;errorMess;</span><br><span class="hljs-comment">    &#125;*/</span><br><br>    <span class="hljs-comment">//设置上传出错信息</span><br>    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getError</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-variable">$str</span> = <span class="hljs-string">&quot;上传文件&lt;span color=&#x27;red&#x27;&gt;<span class="hljs-subst">&#123;$this-&gt;originName&#125;</span>&lt;/span&gt;时出错：&quot;</span>;<br>        <span class="hljs-keyword">switch</span> (<span class="hljs-keyword">$this</span>-&gt;errorNum) &#123;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">4</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;没有文件被上传&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">3</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;文件只有部分被上传&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">2</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> <span class="hljs-number">1</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;上传的文件超过了php.ini中upload_max_filesize选项限制的值&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> -<span class="hljs-number">1</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;未允许的类型&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> -<span class="hljs-number">2</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;文件过大， 上传的文件夹不能超过<span class="hljs-subst">&#123;$this-&gt;maxsize&#125;</span>个字节&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> -<span class="hljs-number">3</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;上传失败&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> -<span class="hljs-number">4</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;建立存放上传文件目录失败，请重新指定上传目录&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br>            <span class="hljs-keyword">case</span> -<span class="hljs-number">5</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;必须指定上传文件的路径&quot;</span>;<br>                <span class="hljs-keyword">break</span>;<br><br>            <span class="hljs-keyword">default</span>:<br>                <span class="hljs-variable">$str</span> .= <span class="hljs-string">&quot;未知错误&quot;</span>;<br>        &#125;<br>        <span class="hljs-keyword">return</span> <span class="hljs-variable">$str</span> . <span class="hljs-string">&quot;&lt;br&gt;&quot;</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//根据文件的相关信息为分块数据创建文件夹</span><br>    <span class="hljs-comment">//md5(当前登录用户的数据库id + 文件原始名称 + 文件类型 + 文件最后修改时间 + 文件总大小)</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setDirNameForChunks</span>(<span class="hljs-params"><span class="hljs-variable">$info</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-variable">$str</span> = <span class="hljs-string">&#x27;&#x27;</span> . <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;userId&#x27;</span>] . <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;name&#x27;</span>] . <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;type&#x27;</span>] . <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;lastModifiedDate&#x27;</span>] . <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;size&#x27;</span>];<br>        <span class="hljs-keyword">return</span> md5(<span class="hljs-variable">$str</span>);<br>    &#125;<br><br>    <span class="hljs-comment">//设置和$_FILES有关的内容</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setFiles</span>(<span class="hljs-params"><span class="hljs-variable">$name</span> = <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-variable">$tmp_name</span> = <span class="hljs-string">&quot;&quot;</span>, <span class="hljs-variable">$size</span> = <span class="hljs-number">0</span>, <span class="hljs-variable">$error</span> = <span class="hljs-number">0</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, <span class="hljs-variable">$error</span>);<br>        <span class="hljs-keyword">if</span> (<span class="hljs-variable">$error</span>) &#123;<br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125;<br>        <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;originName&#x27;</span>, <span class="hljs-variable">$name</span>);<br>        <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;tmpFileName&#x27;</span>, <span class="hljs-variable">$tmp_name</span>);<br>        <span class="hljs-variable">$aryStr</span> = explode(<span class="hljs-string">&quot;.&quot;</span>, <span class="hljs-variable">$name</span>);<br>        <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&quot;fileType&quot;</span>, strtolower(<span class="hljs-variable">$aryStr</span>[count(<span class="hljs-variable">$aryStr</span>) - <span class="hljs-number">1</span>]));<br>        <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&quot;fileSize&quot;</span>, <span class="hljs-variable">$size</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkChunk</span>(<span class="hljs-params"><span class="hljs-variable">$info</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;chunks&#x27;</span>]) &amp;&amp; <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;chunks&#x27;</span>] &gt; <span class="hljs-number">0</span>) &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&quot;isChunk&quot;</span>, <span class="hljs-literal">true</span>);<br><br>            <span class="hljs-keyword">if</span> (<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;chunk&#x27;</span>]) &amp;&amp; <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;chunk&#x27;</span>] &gt;= <span class="hljs-number">0</span>) &#123;<br>                <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&quot;indexOfChunk&quot;</span>, <span class="hljs-variable">$info</span>[<span class="hljs-string">&#x27;chunk&#x27;</span>]);<br><br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>            &#125;<br><br>            <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Exception</span>(<span class="hljs-string">&#x27;分块索引不合法&#x27;</span>);<br>        &#125;<br><br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//设置上传后的文件名称</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">setNewFileName</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;isChunk) &#123;        <span class="hljs-comment">//如果是分块，则以分块的索引作为文件名称保存</span><br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;newFileName&#x27;</span>, <span class="hljs-keyword">$this</span>-&gt;indexOfChunk);<br>        &#125; <span class="hljs-keyword">elseif</span> (<span class="hljs-keyword">$this</span>-&gt;israndname) &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;newFileName&#x27;</span>, <span class="hljs-keyword">$this</span>-&gt;proRandName());<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;newFileName&#x27;</span>, <span class="hljs-keyword">$this</span>-&gt;originName);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">//检查上传的文件是否是合法的类型</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkFileType</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (in_array(strtolower(<span class="hljs-keyword">$this</span>-&gt;fileType), <span class="hljs-keyword">$this</span>-&gt;allowtype)) &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, -<span class="hljs-number">1</span>);<br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">//检查上传的文件是否是允许的大小</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkFileSize</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">$this</span>-&gt;fileSize &gt; <span class="hljs-keyword">$this</span>-&gt;maxsize) &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, -<span class="hljs-number">5</span>);<br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-comment">//检查是否有存放上传文件的目录</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">checkFilePath</span>(<span class="hljs-params"><span class="hljs-variable">$target</span></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">empty</span>(<span class="hljs-variable">$target</span>)) &#123;<br>            <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, -<span class="hljs-number">5</span>);<br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125;<br><br>        <span class="hljs-keyword">if</span> (!file_exists(<span class="hljs-variable">$target</span>) || !is_writable(<span class="hljs-variable">$target</span>)) &#123;<br>            <span class="hljs-keyword">if</span> (!@mkdir(<span class="hljs-variable">$target</span>, <span class="hljs-number">0755</span>)) &#123;<br>                <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, -<span class="hljs-number">4</span>);<br>                <span class="hljs-comment">//return false;</span><br>                show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>            &#125;<br>        &#125;<br><br>        <span class="hljs-keyword">$this</span>-&gt;path = <span class="hljs-variable">$target</span>;<br>        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>    &#125;<br><br>    <span class="hljs-comment">//设置随机文件名</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">proRandName</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-variable">$fileName</span> = date(<span class="hljs-string">&#x27;YmdHis&#x27;</span>) . <span class="hljs-string">&quot;_&quot;</span> . rand(<span class="hljs-number">100</span>, <span class="hljs-number">999</span>);<br>        <span class="hljs-keyword">return</span> <span class="hljs-variable">$fileName</span> . <span class="hljs-string">&#x27;.&#x27;</span> . <span class="hljs-keyword">$this</span>-&gt;fileType;<br>    &#125;<br><br>    <span class="hljs-comment">//复制上传文件到指定的位置</span><br>    <span class="hljs-keyword">private</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">copyFile</span>(<span class="hljs-params"></span>)</span><br><span class="hljs-function">    </span>&#123;<br>        <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">$this</span>-&gt;errorNum) &#123;<br>            <span class="hljs-variable">$path</span> = rtrim(<span class="hljs-keyword">$this</span>-&gt;path, <span class="hljs-string">&#x27;/&#x27;</span>) . <span class="hljs-string">&#x27;/&#x27;</span>;<br>            <span class="hljs-variable">$path</span> .= <span class="hljs-keyword">$this</span>-&gt;newFileName;<br>            <span class="hljs-keyword">if</span> (@move_uploaded_file(<span class="hljs-keyword">$this</span>-&gt;tmpFileName, <span class="hljs-variable">$path</span>)) &#123;<br>                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;<br>            &#125; <span class="hljs-keyword">else</span> &#123;<br>                <span class="hljs-keyword">$this</span>-&gt;setOption(<span class="hljs-string">&#x27;errorNum&#x27;</span>, -<span class="hljs-number">3</span>);<br>                <span class="hljs-comment">//return false;</span><br>                show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>            &#125;<br>        &#125; <span class="hljs-keyword">else</span> &#123;<br>            <span class="hljs-comment">//return false;</span><br>            show_message(<span class="hljs-keyword">$this</span>-&gt;getError(), <span class="hljs-string">&#x27;&#x27;</span>, <span class="hljs-number">2</span>);<br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * 实例</span><br><span class="hljs-comment"> */</span><br><span class="hljs-comment">/**</span><br><span class="hljs-comment">//关闭缓存</span><br><span class="hljs-comment">header(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;);</span><br><span class="hljs-comment">header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);</span><br><span class="hljs-comment">header(&quot;Cache-Control: no-store, no-cache, must-revalidate&quot;);</span><br><span class="hljs-comment">header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);</span><br><span class="hljs-comment">header(&quot;Pragma: no-cache&quot;);</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">$uploader = new FileUpload();</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">//用于断点续传，验证指定分块是否已经存在，避免重复上传</span><br><span class="hljs-comment">if (isset($_POST[&#x27;status&#x27;])) &#123;</span><br><span class="hljs-comment">    if ($_POST[&#x27;status&#x27;] == &#x27;chunkCheck&#x27;) &#123;</span><br><span class="hljs-comment">        $target = &#x27;../uploads/&#x27; . $_POST[&#x27;name&#x27;] . &#x27;/&#x27; . $_POST[&#x27;chunkIndex&#x27;];</span><br><span class="hljs-comment">        if (file_exists($target) &amp;&amp; filesize($target) == $_POST[&#x27;size&#x27;]) &#123;</span><br><span class="hljs-comment">            die(&#x27;&#123;&quot;ifExist&quot;:1&#125;&#x27;);</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        die(&#x27;&#123;&quot;ifExist&quot;:0&#125;&#x27;);</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">    &#125; elseif ($_POST[&#x27;status&#x27;] == &#x27;md5Check&#x27;) &#123;</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        //todo 模拟持久层查询</span><br><span class="hljs-comment">        $dataArr = array(</span><br><span class="hljs-comment">            &#x27;b0201e4d41b2eeefc7d3d355a44c6f5a&#x27; =&gt; &#x27;kazaff2.jpg&#x27;</span><br><span class="hljs-comment">        );</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        if (isset($dataArr[$_POST[&#x27;md5&#x27;]])) &#123;</span><br><span class="hljs-comment">            die(&#x27;&#123;&quot;ifExist&quot;:1, &quot;path&quot;:&quot;&#x27; . $dataArr[$_POST[&#x27;md5&#x27;]] . &#x27;&quot;&#125;&#x27;);</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        die(&#x27;&#123;&quot;ifExist&quot;:0&#125;&#x27;);</span><br><span class="hljs-comment">    &#125; elseif ($_POST[&#x27;status&#x27;] == &#x27;chunksMerge&#x27;) &#123;</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">        if ($path = $uploader-&gt;chunksMerge($_POST[&#x27;name&#x27;], $_POST[&#x27;chunks&#x27;], $_POST[&#x27;ext&#x27;])) &#123;</span><br><span class="hljs-comment">            //todo 把md5签名存入持久层，供未来的秒传验证</span><br><span class="hljs-comment">            die(&#x27;&#123;&quot;status&quot;:1, &quot;path&quot;: &quot;&#x27; . $path . &#x27;&quot;&#125;&#x27;);</span><br><span class="hljs-comment">        &#125;</span><br><span class="hljs-comment">        die(&#x27;&#123;&quot;status&quot;:0&#x27;);</span><br><span class="hljs-comment">    &#125;</span><br><span class="hljs-comment">&#125;</span><br><span class="hljs-comment"></span><br><span class="hljs-comment">if (($path = $uploader-&gt;upload(&#x27;file&#x27;, $_POST)) !== false) &#123;</span><br><span class="hljs-comment">    die(&#x27;&#123;&quot;status&quot;:1, &quot;path&quot;: &quot;&#x27; . $path . &#x27;&quot;&#125;&#x27;);</span><br><span class="hljs-comment">&#125;</span><br><span class="hljs-comment">die(&#x27;&#123;&quot;status&quot;:0&#125;&#x27;);</span><br><span class="hljs-comment"> */</span><br></code></pre></td></tr></table></figure>

<h1 id="最后"><a href="#最后" class="headerlink" title="最后"></a>最后</h1><p>发现自己语言组织能力好差啊，各位看官还是转到<a target="_blank" rel="noopener" href="http://blog.kazaff.me/2014/11/14/%E8%81%8A%E8%81%8A%E5%A4%A7%E6%96%87%E4%BB%B6%E4%B8%8A%E4%BC%A0/">聊聊大文件上传</a>去欣赏吧</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/PHP/">PHP</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/PHP/">PHP</a>
                    
                      <a class="hover-with-bg" href="/tags/webuploader/">webuploader</a>
                    
                      <a class="hover-with-bg" href="/tags/js/">js</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2017/05/22/php/%E9%A9%BC%E5%B3%B0%E5%91%BD%E5%90%8D%E6%B3%95%E5%92%8C%E4%B8%8B%E5%88%92%E7%BA%BF%E9%A3%8E%E6%A0%BC%E7%9A%84%E4%BA%92%E8%BD%AC/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">【转】驼峰命名法和下划线风格的互转</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2017/04/02/mysql/%E9%85%8D%E7%BD%AEphpMyAdmin%E8%BF%9E%E6%8E%A5%E5%A4%9A%E5%8F%B0mysql%E6%9C%8D%E5%8A%A1%E5%99%A8/">
                        <span class="hidden-mobile">配置phpmyadmin使登录时可填写IP管理多台MySQL 连接多个数据库 自动登录</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
